package com.marchsoft.group.manager.security.Filter;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import java.io.IOException;

@Component
public class ExceptionFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        try {
            chain.doFilter(request, response);
        } catch (Exception e) {
            System.out.println("错误信息");
//           e.printStackTrace();
            request.setAttribute("exception", e);
            request.getRequestDispatcher("/error").forward(request, response);
        }
    }
    @Bean
    public FilterRegistrationBean<ExceptionFilter> exceptionFilterRegistration() {
        FilterRegistrationBean<ExceptionFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(new ExceptionFilter());
        registration.setName("exceptionFilter");
        /* 这个序号要很小，保证 exceptionFilter 是所有过滤器链的入口 */
        registration.setOrder(Integer.MIN_VALUE);
        return registration;
    }
}